<?

 class Controller_Profile extends Controller_Base_Secret {
 	
 	function index($args) {
 		$template = new Template('profile.tmpl'); 
	
	 	$sigs = array(
	 		'action'	=> array('type' => 'string','function'=>array('htmlspecialchars'),'required' =>1),
	 		'password'	=> array('type' => 'string','function'=>array(),'required' =>1),
	 		'password2'	=> array('type' => 'string','function'=>array(),'required' =>1),
			'email'		=> array('type' => 'string','function'=>array('htmlspecialchars'),'required' =>1),
			'realname'	=> array('type' => 'string','function'=>array('htmlspecialchars'),'required' =>1),
		);			
	
		sanitize_vars($_POST,$sigs,null);
	
		if($_POST[action] === 'save') {
			try{
				$result = $this->user->updateUser($_POST[realname],$_POST[email],$this->user->id);
			} catch(UserException $e) {
				error_log("ERROR: ".$e->getMessage());
				$template->setArg('profile_error',$e->getMessage());
			}
		
			if($result)
				return array(status => 302, location => '/profile');
		}
		
		if($_POST[action] === 'change_password') {
			try{
				$this->user->changePassword($_POST[password],$_POST[password2],$user->id);
			} catch(UserException $e) {
				$template->setArg('profile_error_text',$e->getMessage());
			}
			
			if($result)
				return array(status => 302, location => '/profile');
		}		
		
 		$template->setArgs(
 			array( 
				_user		=> $this->user,
				email		=> $this->user->email,
				realname	=> $this->user->realname,
				user_id		=> $this->user->id,
				menu		=> $this->menu->getMenu(1),
				phone		=> $this->user->phone,
			)
		);
 		
 		return array(
 			status 		=> 0,
 			template	=> $template
 		);
 	}
 }